﻿using System;

namespace DO.DreamOnTrader.Quote
{

	/// <summary>
	/// 即時報價接收器
	/// </summary>
	public abstract class RealTimeTickLoader : TickLoader
	{
		DateTime quoteDay = DateTime.Today;
		string quoteMonth = "";
		long allqty = 0;

		private void axiPushX_SetData3(object sender, AxIPUSHXLib._DiPushXEvents_SetData3Event e)
		{
			int rtn;

			rtn = axXQuote.SetData3(e.vChannel, e.vData);
			if (rtn == (int)QuoteType.期貨市況 || rtn == (int)QuoteType.股票市況 || rtn == (int)QuoteType.選擇權市況)
			{

				if (axXQuote.GetIntValue(19) != allqty)
				{
					

					string quoteMonthPlus = "";
					#region 處理axXQuote

					float price = axXQuote.GetIntValue(16) / 1000;
					allqty = axXQuote.GetIntValue(19);
					uint qty = (uint)axXQuote.GetIntValue(18);
					string time = axXQuote.GetIntValue(3).ToString().PadLeft(8, '0');
					DateTime TradeTime = quoteDay.AddHours(Convert.ToInt32(time.Substring(0, 2))).AddMinutes(Convert.ToInt32(time.Substring(2, 2))).AddSeconds(Convert.ToInt32(time.Substring(4, 2)));
					string CloseMonth = axXQuote.GetStrValue(0).Substring(axXQuote.GetStrValue(0).Length - 6);

					#endregion

					Tick tick = new Tick(price, filterProduct, qty, TradeTime, CloseMonth, allqty);

					if (checkCloseMonth(tick))
					{
						//報價的交易時間仍沒換月
						proecessTick(tick);
					}
					else
					{
						//報價的交易時間己達user設定的換月時間
						quoteMonthPlus = getQuoteMonth(tick.TradeTime);
						axiPushX.ipushUnsub("x" + filterProduct.QuoteName + quoteMonth);
						//訂閱換月月份
						axiPushX.ipushSub("x" + filterProduct.QuoteName + quoteMonthPlus);
					}
				}
			}
		}

		#region =====Quote 成員

		public override bool Start()
		{
			if (axiPushX.ipushConnect() == 1)
			{
				if (DateTime.Now.Hour < 8)
				{
					quoteMonth = getQuoteMonth(quoteDay.AddDays(-1));
				}
				else
				{
					quoteMonth = getQuoteMonth(quoteDay);
				}
				axiPushX.ipushSub("x" + filterProduct.QuoteName + quoteMonth);
				return true;
			}
			else
			{
				return false;
			}
		}

		public override bool Stop()
		{
			axiPushX.ipushDisconnect();
			return true;
		}

		#endregion

		AxXQUOTELib.AxXQuote axXQuote;
		AxIPUSHXLib.AxiPushX axiPushX;

		#region ===元件參數

		const string iPush_IP = "202.39.34.43";
		const int iPush_Port = 443;
		const string iPush_Company = "masterlink";
		const string iPush_Product = "xQuote";
		const string iPush_UserName = "EC642";
		const string iPush_Password = "ml2856";
		const int iPush_UsingSetData = 3;

		#endregion

		public RealTimeTickLoader(Product _product, int _addQuoteMonth = 0, int _filterMinute = 30)
		{
			filterProduct = _product;
			addQuoteMonth = _addQuoteMonth;
			filterMinute = _filterMinute;

			this.axiPushX = new AxIPUSHXLib.AxiPushX();
			this.axXQuote = new AxXQUOTELib.AxXQuote();

			//----------------控制項必須---------------------//
			#region 此控制項必須的程式碼
			((System.ComponentModel.ISupportInitialize)(this.axiPushX)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.axXQuote)).BeginInit();

			Controls.Add(this.axiPushX);
			Controls.Add(this.axXQuote);

			((System.ComponentModel.ISupportInitialize)(this.axiPushX)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.axXQuote)).EndInit();
			#endregion
			//----------------------------------------------//

			axiPushX.SetData3 += new AxIPUSHXLib._DiPushXEvents_SetData3EventHandler(this.axiPushX_SetData3);

			axiPushX.ipuship = iPush_IP;
			axiPushX.ipushport = iPush_Port;
			axiPushX.company = iPush_Company;
			axiPushX.product = iPush_Product;
			axiPushX.username = iPush_UserName;
			axiPushX.password = iPush_Password;
			axiPushX.usingSetData = iPush_UsingSetData;

		}
	}
}
